// Copyright (c) Microsoft Corporation.  All rights reserved. 

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Data.OleDb;

namespace PCDimNaturalizer
{
    static class Program
    {
        public static frmASFlattener ASFlattener;
        public static frmProgress Progress;
        public static frmSQLFlattener SQLFlattener;
        public static string LogFile = null;

        [STAThread]
        static void Main(string[] args)
        {
            bool ShowInterface = false, SQLSource = false;
            string Server = null, Database = null, Dimension = null, Table = null, ID = null, PID = null;
            List<string> NonPCHierarchies = new List<string>(), PCAttributes = new List<string>(), SQLColsAsPCAttributes = new List<string>(), SQLColsAsNonPCAttributes = new List<string>();
            int MinLevels = 0, ActionLevel = 4; // 4 is just default action level.

            for (int i = 0; i < args.Length; i++)
                switch (args[i].ToLower().Split('=')[0])
                {
                    // General Options
                    case "/?":
                        // If the help file is missing from the install directory for some reason, save it from internal resource kept for that reason.
                        try
                        {
                            if (!File.Exists(Environment.CurrentDirectory + "\\PCDimensionNaturalizer.chm") ||
                                File.ReadAllBytes(Environment.CurrentDirectory + "\\PCDimensionNaturalizer.chm").Length != Properties.Resources.Help.Length)
                                File.WriteAllBytes(Environment.CurrentDirectory + "\\PCDimensionNaturalizer.chm", Properties.Resources.Help);
                            System.Diagnostics.Process.Start("hh.exe", Environment.CurrentDirectory + "\\PCDimensionNaturalizer.chm::Overview.htm#_Toc203290469");
                        }
                        catch (Exception) { }
                        return;
                    case "/logfile":
                        LogFile = args[i].Split('=')[1];
                        break;
                    case "/showui":
                        ShowInterface = true;
                        break;
                    // Naturalization options for both SQL and AS
                    case "/minlevels":
                        MinLevels = Convert.ToInt32(args[i].Split('=')[1]);
                        break;
                    case "/server":
                        Server = args[i].Split('=')[1];
                        break;
                    case "/database":
                        Database = args[i].Split('=')[1];
                        break;
                    // AS Specific Options
                    case "/dimension":
                        Dimension = args[i].Split('=')[1];
                        break;
                    case "/pcattributes":
                        PCAttributes.AddRange(args[i].Split('=')[1].Split(','));
                        break;
                    case "/nonpchierarchies":
                        NonPCHierarchies.AddRange(args[i].Split('=')[1].Split(','));
                        break;
                    case "/actionlevel":
                        ActionLevel = Convert.ToInt32(args[i].Split('=')[1]);
                        break;
                    // SQL Specific Options
                    case "/sqlsource":
                        SQLSource = true;
                        break;
                    case "/sqltable":
                        Table = args[i].Split('=')[1];
                        break;
                    case "/sqlidcolumn":
                        ID = args[i].Split('=')[1];
                        break;
                    case "/sqlparentidcolumn":
                        PID = args[i].Split('=')[1];
                        break;
                    case "/sqlcolumnsaspcattributes":
                        SQLColsAsPCAttributes.AddRange(args[i].Split('=')[1].Split(','));
                        break;
                    case "/sqlcolumnsasnonpcattributes":
                        SQLColsAsNonPCAttributes.AddRange(args[i].Split('=')[1].Split(','));
                        break;
                    default:
                        break;
                }

            PCDimNaturalizer.LogFile = LogFile;
            PCDimNaturalizer.LogText("____________________________");
            PCDimNaturalizer.LogText("Starting PCDimFlattener...");
            PCDimNaturalizer.LogText("Time reported in GMT = EST+5 = CST+6 = PST+8");

            if (args.Length == 0 || ShowInterface || (args.Length == 1 && SQLSource))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                if (SQLSource)
                {
                    SQLFlattener = new frmSQLFlattener();
                    SQLFlattener.MinLevels = MinLevels;
                    if (Server != null)
                        SQLFlattener.txtServer.Text = Server;
                    if (Database != null)
                        SQLFlattener.cmbDatabase.Items[0] = Database;
                    if (Table != null)
                        SQLFlattener.cmbTable.Items[0] = Table;
                    if (ID != null)
                        SQLFlattener.cmbID.Items[0] = ID;
                    if (Table != null)
                        SQLFlattener.cmbPID.Items[0] = PID;
                    if (SQLColsAsPCAttributes.Count > 0) SQLFlattener.AddAllAttributesPC = false;
                    SQLFlattener.AttributesPC = SQLColsAsPCAttributes;
                    SQLFlattener.AttributesNatural = SQLColsAsNonPCAttributes;
                    Application.Run(SQLFlattener);
                }
                else
                {
                    ASFlattener = new frmASFlattener();
                    if (Server != null)
                        ASFlattener.txtServer.Text = Server;
                    if (Database != null)
                        ASFlattener.cmbDatabase.Items[0] = Database;
                    if (Dimension != null)
                        ASFlattener.cmbDimension.Items[0] = Dimension;
                    ASFlattener.MinLevels = MinLevels;
                    ASFlattener.ActionLevel = ActionLevel;
                    if (NonPCHierarchies.Count > 0) ASFlattener.AddAllNonPCHierarchies = false;
                    if (PCAttributes.Count > 0) ASFlattener.AddAllPCAttributes = false;
                    ASFlattener.NonPCHierarchiesToInclude = NonPCHierarchies;
                    ASFlattener.PCAttributesToInclude = PCAttributes;
                    Application.Run(ASFlattener);
                }
            }
            else
            {
                try
                {
                    PCDimNaturalizer nat;
                    if (SQLSource)
                    {
                        OleDbConnection conn = new OleDbConnection("Provider=SQLOLEDB;Integrated Security=SSPI;Data Source=" + Server + ";Initial Catalog=" + Database);
                        conn.Open();
                        nat = new SQLPCDimNaturalizer(conn, Table, ID, PID, MinLevels);
                    }
                    else
                        nat = new ASPCDimNaturalizer(Server, Database, Dimension, ActionLevel);
                    nat.Naturalize(MinLevels);
                }
                catch (Exception e)
                {
                    PCDimNaturalizer.LogText(e.ToString());
                }
            }
        }

    }
}